package com.smallkingson.ylib.core.utils;

import java.util.HashMap;
import java.util.Map;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("NewApi")
public class YPopupWindow {

	public PopupWindow popupWindow;

	private RelativeLayout popupView;

	private View containerView;

	private View fgView;

	private Context context;

	private View bgView;

	private Map<String, Object> tags;

	private Object tag;

	private OnStateChangeListener onStateChangeListener = new OnStateChangeListener() {

		@Override
		public boolean onShow(YPopupWindow popupWindow, View fgView, View bgView) {
			return false;
		}

		@Override
		public boolean onDismiss(YPopupWindow popupWindow, View fgView,
				View bgView) {
			return false;
		}
	};

	private YPopupWindow(RelativeLayout popupView, View containerView) {

		this.popupView = popupView;
		this.containerView = containerView;

	}

	private YPopupWindow(Context context) {
		this.context = context;
	}
	public YPopupWindow(Context context, View popupView,
						View showInTheView) {
		if (!(popupView instanceof RelativeLayout)) {

			throw new RuntimeException("只支持 RelativeLayout");
		}
		this.context = context;
		this.popupView = (RelativeLayout) popupView;

		init((RelativeLayout) popupView, showInTheView);
	}

	private void init(RelativeLayout popupView, View showInTheView) {

		tags = new HashMap<String, Object>();
		this.popupView = popupView;
		this.containerView = showInTheView;

		popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT,true);

		popupWindow.setFocusable(true);
		popupView.setFocusable(true);
		popupView.setFocusableInTouchMode(true);
		popupView.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN
						&& keyCode == KeyEvent.KEYCODE_BACK)
					dismiss();
				return false;
			}
		});

		setFgView(popupView.getChildAt(0));

		getFgView().setVisibility(View.GONE);

		addBgView();
	}

	private void  addBgView() {
		setBgView(new View(context));

		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

		getBgView().setClickable(true);

		getBgView().setBackgroundColor(0x33000000);

		getBgView().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				dismiss();
			}
		});
		popupView.addView(getBgView(), 0, lp);
	}

	public void show() {
		popupWindow.showAtLocation(containerView, Gravity.CENTER, 0, 0);
		getFgView().setVisibility(View.VISIBLE);
		if (getOnStateChangeListener().onShow(this, getFgView(), getBgView())) {
			return;
		}
		showFgView();
	}

	protected void showFgView() {

		ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 1.0f).setDuration(
				500);

//		animator.setInterpolator(new AnticipateOvershootInterpolator());

		animator.addUpdateListener(new AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator animation) {

				float animatedFraction = (Float) animation.getAnimatedValue();

				getFgView().setAlpha(animatedFraction);
			}
		});
		animator.start();
	}

	public void dismiss() {

		if (onStateChangeListener.onDismiss(this, getFgView(), getBgView())) {
			return;
		}
		performDismiss();
	}

	public void performDismiss() {
		getFgView().setVisibility(View.GONE);
		popupWindow.dismiss();

	}

	public boolean isShowing() {
		return popupWindow.isShowing();
	}

	public YPopupWindow setOnClickListener(int id, OnClickListener clickListener) {
		popupView.findViewById(id).setOnClickListener(clickListener);
		return this;
	}

	public View findViewById(int id) {
		return popupView.findViewById(id);
	}

	public String getText(int id) {

		TextView textView = (TextView) popupView.findViewById(id);

		return textView.getText().toString();
	}

	/**
	 * @return the onStateChangeListener
	 */
	public OnStateChangeListener getOnStateChangeListener() {
		return onStateChangeListener;
	}

	/**
	 * @param onStateChangeListener
	 *            the onStateChangeListener to set
	 */
	public void setOnStateChangeListener(
			OnStateChangeListener onStateChangeListener) {
		this.onStateChangeListener = onStateChangeListener;
	}

	/**
	 * @return the tags
	 */
	public Object getTag(String key) {
		return tags.get(key);
	}

	/**
	 *            the tags to set
	 */
	public void setTags(String key, Object value) {
		tags.put(key, value);
	}

	/**
	 * @return the bgView
	 */
	public View getBgView() {
		return bgView;
	}

	/**
	 * @param bgView
	 *            the bgView to set
	 */
	private void setBgView(View bgView) {
		this.bgView = bgView;
	}

	/**
	 * @return the fgView
	 */
	public View getFgView() {
		return fgView;
	}

	/**
	 * @param fgView
	 *            the fgView to set
	 */
	private void setFgView(View fgView) {
		this.fgView = fgView;
	}

	/**
	 * @return the tag
	 */
	public Object getTag() {
		return tag;
	}

	/**
	 * @param tag
	 *            the tag to set
	 */
	public void setTag(Object tag) {
		this.tag = tag;
	}

	public static interface OnStateChangeListener {

		boolean onShow(YPopupWindow popupWindow, View fgView, View bgView);

		boolean onDismiss(YPopupWindow popupWindow, View fgView, View bgView);
	}
}